package com.zhiwang.planet.mvvm.viewmodel

import android.text.TextUtils
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.zhiwang.planet.BR
import com.zhiwang.planet.R
import com.zhiwang.planet.di.ActivityScope
import com.zhiwang.planet.mvvm.model.RestApi
import com.zhiwang.planet.mvvm.model.entity.main.Activity
import com.zhiwang.planet.mvvm.model.entity.main.FeedBack
import com.zhiwang.planet.mvvm.model.entity.main.SystemNotification
import com.zhiwang.planet.ui.WebViewActivity
import com.zhiwang.planet.ui.my.message.FeedBackDetailActivity
import com.zhiwang.planet.util.arrange
import io.reactivex.functions.Consumer
import me.tatarka.bindingcollectionadapter2.ItemBinding
import java.util.*
import javax.inject.Inject


@ActivityScope
class ActivityViewModel @Inject constructor(var restApi: RestApi) : ViewModel() {


    val itemBinding = ItemBinding.of<Activity>(BR.viewModel, R.layout.item_activity)

    val items = MutableLiveData<List<Activity>>()

    val visibility = MutableLiveData<Int>(View.GONE)

    fun load() {

        restApi.getActivityList()
                .arrange(Consumer {
                    if (it.isEmpty())
                        visibility.value = View.VISIBLE
                    else visibility.value = View.GONE
                    items.value = it

                }, {

                })
    }
}


@ActivityScope
class SystemNotificationViewModel @Inject constructor(var restApi: RestApi, var activity: AppCompatActivity) : ViewModel() {


    val visibility = MutableLiveData<Int>(View.GONE)


    val onItemClick = object : OnItemClickListener<String> {
        override fun onItemClick(payload: String) {
            if (TextUtils.isEmpty(payload).not())
                WebViewActivity.load(activity, payload)
        }
    }

    val itemBinding = ItemBinding.of<SystemNotification>(BR.viewModel, R.layout.item_system_notification)
            .bindExtra(BR.listener, onItemClick)

    val items = MutableLiveData<List<SystemNotification>>()

    fun load() {

        restApi.getSystemNotificationList()
                .arrange(Consumer {

                    if (it.isEmpty())
                        visibility.value = View.VISIBLE
                    else visibility.value = View.GONE

                    items.value = it

                }, {

                })
    }
}


interface OnItemClickListener<T> {

    fun onItemClick(payload: T)
}

@ActivityScope
class FeedbackListViewModel @Inject constructor(var restApi: RestApi, var activity: AppCompatActivity) : ViewModel() {


    val visibility = MutableLiveData<Int>(View.GONE)


    val onItemClick = object : OnItemClickListener<FeedBack> {
        override fun onItemClick(payload: FeedBack) {
            FeedBackDetailActivity.start(activity, payload)
        }
    }

    val itemBinding = ItemBinding.of<FeedBack>(BR.viewModel, R.layout.item_feedback_list)
            .bindExtra(BR.listener, onItemClick)


    val items = MutableLiveData<List<FeedBack>>()

    fun load() {

        restApi.getFeedBackList()
                .arrange(Consumer {

                    if (it.isEmpty())
                        visibility.value = View.VISIBLE
                    else visibility.value = View.GONE
                    items.value = it

                }, {

                })
    }

}